package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import user.User;
import externaldata.gareUIC.GareUIC;

public class DaoGareUIC extends DAO<GareUIC>{



	@Override
	public List<GareUIC> find(String id) {
		Session session = HibernateUtil.openSession();
		Transaction tr = null;
		Criteria criteria = session.createCriteria(GareUIC.class);
		List<GareUIC> rep = null;
		try{
			tr = session.beginTransaction();
			criteria.add(Restrictions.eq("UIC", id));
			rep = criteria.list();
			tr.commit();
		}catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return rep;
	}

	public GareUIC findOne(String uic) {
		Session session = HibernateUtil.openSession();
		Transaction tr = null;
		Criteria criteria = session.createCriteria(GareUIC.class);
		List<GareUIC> rep = null;
		try{
			tr = session.beginTransaction();
			criteria.add(Restrictions.eq("UIC", uic));
			rep = criteria.list();
			tr.commit();
		}catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		if(rep.size() != 0)
			return rep.get(0);
		return null;
	}

	@Override
	public Boolean create(GareUIC obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean update(GareUIC obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean delete(GareUIC obj) {
		return null;
		// TODO Auto-generated method stub

	}

	public Boolean saveAll(List<GareUIC> listgare){
		Session session = HibernateUtil.openSession();
		Transaction tr = null;
		try{
			tr = session.beginTransaction();

			for(int i = 0; i < listgare.size(); i++){
				session.save(listgare.get(i));
				//TODO check jdbc batch size
				if(i % 20 == 0) {
					session.flush();
					session.clear();
				}
			}

			tr.commit();
			return true;
		}catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

}
